package demo1;

import java.util.Arrays;

/**
 * @Author liangzai
 * @Description:
 */
public class Training {
    //将整数按权重排序
    public int getKth(int lo, int hi, int k) {
        int[] arr = new int[hi-lo+1];
        for (int i=lo;i<=hi;i++){
            int n=i;
            int step=0;
            while (n!=1){
                step++;
                if (n%2==0){
                    n/=2;
                }else{
                    n=n*3+1;
                }
            }
            arr[i-lo]=step;
        }
        int[] ori = new int[arr.length];
        for (int i=0;i<arr.length;i++){
            ori[i]=arr[i];
        }
        Arrays.sort(arr);

//数前面有多少个相同的数字，有几个就多移几位
        int test=-1;
        for (int i=k-1;i>=0;i--){
            if (arr[i]!=arr[k-1])break;
            test++;
        }
        for (int i=0;i<arr.length;i++){
            if (arr[k-1]==ori[i]){
                if (test>0){
                    test--;
                }else{
                    return lo+i;
                }
            }
        }
        return 114514;
    }
}
